package com.show.business.device.controller;
import com.show.business.device.bean.DeploymentLocation;
import com.show.business.device.service.IDeploymentLocationService;
import com.show.business.common.annotation.Log;
import com.show.business.common.core.controller.BaseController;
import com.show.business.common.core.domain.AjaxResult;
import com.show.business.common.enums.BusinessType;
import com.show.business.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 设备 前端控制器
 * </p>
 *
 * @author ccx
 * @since 2023-10-13
 */
@Api(value = "设备-部署位置",description = "设备-部署位置")
@RestController
@RequestMapping("/device/deploymentLocation")
public class DeploymentLocationController extends BaseController {

    @Resource
    private IDeploymentLocationService deploymentLocationService;

    /**
     * 获取部署位置列表
     */
    @ApiOperation("获取部署位置列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", dataType = "Integer", dataTypeClass = Integer.class)
    })
    @GetMapping("/list")
    public AjaxResult list(DeploymentLocation deploymentLocation)
    {
        List<DeploymentLocation> deploymentLocations = deploymentLocationService.getList(deploymentLocation);
        return success(deploymentLocations);
    }

    /**
     * 查询部署位置列表（排除节点）
     */
    @GetMapping("/list/exclude/{id}")
    public AjaxResult excludeChild(@PathVariable(value = "id", required = false) String classId)
    {
        List<DeploymentLocation> goodsClasses = deploymentLocationService.getList(new DeploymentLocation());
        goodsClasses.removeIf(d -> d.getId() .equals(classId) || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), classId + ""));
        return success(goodsClasses);
    }

    /**
    /**
     * 根据部署位置编号获取详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable String id)
    {
        return success(deploymentLocationService.selectDeploymentLocationById(id));
    }

    /**
     * 新增部署位置
     */
    @Log(title = "部署位置", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@Validated @RequestBody DeploymentLocation deploymentLocation)
    {
        if (!deploymentLocationService.checkDeploymentLocationNameUnique(deploymentLocation))
        {
            return error("新增部署位置'" + deploymentLocation.getName() + "'失败，部署位置名称已存在");
        }
        deploymentLocation.setCreateBy(getUsername());
        deploymentLocation.setDelFlag("0");
        return deploymentLocationService.insertDeploymentLocation(deploymentLocation);
    }

    /**
     * 修改部署位置
     */
    @Log(title = "部署位置", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@Validated @RequestBody DeploymentLocation deploymentLocation)
    {
        String classId = deploymentLocation.getId();
        if (!deploymentLocationService.checkDeploymentLocationNameUnique(deploymentLocation)){
            return error("修改部署位置'" + deploymentLocation.getName() + "'失败，部署位置名称已存在");
        }
        if (!deploymentLocation.getLocationType().equals("0") && deploymentLocation.getParentId().equals(classId)){
            return error("修改部署位置'" + deploymentLocation.getName() + "'失败，上级部署位置不能是自己");
        }
        deploymentLocation.setUpdateBy(getUsername());
        return deploymentLocationService.updateDeploymentLocation(deploymentLocation);
    }

    /**
     * 删除部署位置
     */
    @Log(title = "部署位置", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{id}")
    public AjaxResult remove(@PathVariable String id)
    {
        if (deploymentLocationService.hasChildByDeploymentLocationId(id))
        {
            return warn("存在下级部署位置,不允许删除");
        }
        return deploymentLocationService.delInfo(id);
    }
    /**
     * 获取路段
     */
    @ApiOperation("获取路段")
    @GetMapping("/getDeploymentLocationByLocationType")
    public Object getDeploymentLocationByLocationType(@RequestParam String type) {
        return AjaxResult.success(deploymentLocationService.checkDeploymentLocationByLocationType(type));
    }
    /**
     * 获取场地/位置
     */
    @ApiOperation("获取场地/位置")
    @GetMapping("/getChildrenListByParentId")
    public Object getChildrenListByParentId(@RequestParam String parentId) {
        return AjaxResult.success(deploymentLocationService.getChildrenListByParentId(parentId));
    }

    /**
     * 获取部署位置
     */
    @ApiOperation("获取部署位置")
    @GetMapping("/deploymentLocationAllList")
    public Object deploymentLocationAllList() {
        return AjaxResult.success(deploymentLocationService.getDeploymentLocationList());
    }

    /**
     * 获取部署位置下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(DeploymentLocation deploymentLocation)
    {
        List<DeploymentLocation> classList = deploymentLocationService.getList(deploymentLocation);
        return AjaxResult.success(deploymentLocationService.buildDeploymentLocationTreeSelect(classList));
    }
    /**
     * 获取部署位置树
     */
    @GetMapping("/deploymentLocationTree")
    public AjaxResult deploymentLocationTree(DeploymentLocation deploymentLocation)
    {
        return success(deploymentLocationService.selectDeploymentLocationTreeList(deploymentLocation));
    }
}
